//
//  ChatViewController.m
//  Frame
//
//  Created by 这是工作本 on 16/1/28.
//  Copyright © 2016年 这是工作本. All rights reserved.
//

#import "ChatViewController.h"

@interface ChatViewController () <UITextFieldDelegate, UIGestureRecognizerDelegate>
@property (nonatomic, retain) UIView *backV;

@property (nonatomic, retain) UIButton *voiceChange;

@property (nonatomic, retain) UIButton *voice;

@property (nonatomic, retain) UITextField *chatfiled;

@property (nonatomic, retain) UIButton *moji;

@property (nonatomic, retain) UIButton *moreFunction;

@property (nonatomic, assign) BOOL clickVoice;

@property (nonatomic, assign) BOOL clickMoji;

@property (nonatomic, assign) BOOL clickMoreFunction;

@property (nonatomic, retain) UIView *backVoice;

@property (nonatomic, retain) NSTimer *myTimer;

@property (nonatomic, retain) UILabel *la ;

@property (nonatomic, assign) BOOL textOrvoice;
@end

@implementation ChatViewController

- (void)viewWillAppear:(BOOL)animated
{
    [self.backVoice removeFromSuperview];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.backVoice removeFromSuperview];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.navigationItem.title = @"聊天";
    //隐藏自动返回
    self.navigationItem.hidesBackButton = YES;
    //返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"矩形-3@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    
    //点击输入框位置改变
    self.clickVoice = YES;
    
    self.clickMoji = YES;
    
    self.clickMoreFunction = YES;
    
    
    
    //设置title颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1.0],NSForegroundColorAttributeName,nil]];
    
    //聊天背景
    self.backV = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 40 * ScreenHeigth, [UIScreen mainScreen].bounds.size.width, 40 * ScreenHeigth)];
    self.backV.backgroundColor = [UIColor colorWithRed:200 / 255.0 green:200 / 255.0 blue:200 / 255.0 alpha:1.0];
    [self.view addSubview:self.backV];
    
    //语音切换
    self.voiceChange = [UIButton buttonWithType:UIButtonTypeCustom];
    self.voiceChange.frame = CGRectMake(5 * ScreenWidth, 5 * ScreenHeigth , 30 * ScreenWidth, 30 * ScreenHeigth);
    [self.voiceChange setImage:[UIImage imageNamed:@"iconfont-shengyin.png"] forState:UIControlStateNormal];
    [self.voiceChange addTarget:self action:@selector(voiceChangeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.backV addSubview:self.voiceChange];
    
    //输入框
    self.chatfiled = [[UITextField alloc] initWithFrame:CGRectMake(self.voiceChange.frame.origin.x + self.voiceChange.frame.size.width + 5 * ScreenWidth, self.voiceChange.frame.origin.y , [UIScreen mainScreen].bounds.size.width - (self.voiceChange.frame.origin.x + self.voiceChange.frame.size.width) * 3 , self.voiceChange.frame.size.height)];
    self.chatfiled.layer.cornerRadius = 5 * ScreenWidth;
    self.chatfiled.layer.masksToBounds = YES;
    //设置输入键盘类型
    self.chatfiled.keyboardType = UIKeyboardTypeNamePhonePad;
    self.chatfiled.backgroundColor = [UIColor whiteColor];
    self.chatfiled.delegate = self;
    [self.backV addSubview:self.chatfiled];
    
    //语音
    self.voice = [UIButton buttonWithType:UIButtonTypeCustom];
    self.voice.frame = CGRectMake(self.voiceChange.frame.origin.x + self.voiceChange.frame.size.width + 5 * ScreenWidth, self.voiceChange.frame.origin.y , [UIScreen mainScreen].bounds.size.width - (self.voiceChange.frame.origin.x + self.voiceChange.frame.size.width) * 3 , self.voiceChange.frame.size.height);
    [self.voice setTitle:@"按住 说话" forState:UIControlStateNormal];
    [self.voice setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.voice.backgroundColor = [UIColor orangeColor];
    //长按事件
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(btnLong:)];
    longPress.minimumPressDuration = 0.5; //定义按的时间
    [self.voice addGestureRecognizer:longPress];

    //点击事件
    [self.voice addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    //语音说话图标
    
    self.backVoice = [[UIView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 50 * ScreenHeigth, ([UIScreen mainScreen].bounds.size.height - 113) / 2 - 50 * ScreenHeigth, 100 * ScreenWidth, 100 * ScreenHeigth)];
    self.backVoice.backgroundColor = [UIColor grayColor];
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20 * ScreenWidth, 10 * ScreenHeigth, 60 * ScreenWidth, 60 * ScreenHeigth)];
    image.image = [UIImage imageNamed:@"iconfont-yuyin.png"];
    [self.backVoice addSubview:image];
    
    
    self.la = [[UILabel alloc] initWithFrame:CGRectMake(0, image.frame.origin.y + image.frame.size.height + 5 * ScreenHeigth, 100, 20)];
    self.la.text = @"手指上划取消发送";
    self.la.font = [UIFont systemFontOfSize:12.0];
    [self.backVoice addSubview:self.la];
    
    
    
    //moji
    
    self.moji = [UIButton buttonWithType:UIButtonTypeCustom];
    self.moji.frame = CGRectMake(self.chatfiled.frame.origin.x + self.chatfiled.frame.size.width + 5 * ScreenWidth, self.voiceChange.frame.origin.y , self.voiceChange.frame.size.width, self.voiceChange.frame.size.height);
    [self.moji setImage:[UIImage imageNamed:@"iconfont-biaoqing.png"] forState:UIControlStateNormal];
    [self.moji addTarget:self action:@selector(mojiAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.backV addSubview:self.moji];
    
    //更多
    
    self.moreFunction = [UIButton buttonWithType:UIButtonTypeCustom];
    self.moreFunction.frame = CGRectMake(self.moji.frame.origin.x + self.moji.frame.size.width, self.voiceChange.frame.origin.y , self.voiceChange.frame.size.width + 5 * ScreenWidth, self.voiceChange.frame.size.height);
    [self.moreFunction setImage:[UIImage imageNamed:@"iconfont-gengduo.png"] forState:UIControlStateNormal];
    [self.moreFunction addTarget:self action:@selector(moreFunctionAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.backV addSubview:self.moreFunction];
    
    
    //点击空白回收键盘
    UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesAction)];
    [self.view addGestureRecognizer:ges];
    
    
    
    
}


//开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.backV = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 218 * ScreenHeigth, [UIScreen mainScreen].bounds.size.width, 40 * ScreenHeigth)];
    self.backV.backgroundColor = [UIColor colorWithRed:200 / 255.0 green:200 / 255.0 blue:200 / 255.0 alpha:1.0];
    [self.view addSubview:self.backV];
    
    [self.backV addSubview:self.voiceChange];
    [self.backV addSubview:self.chatfiled];
    [self.backV addSubview:self.moji];
    [self.backV addSubview:self.moreFunction];
    
}


//回收键盘
- (void)gesAction
{
    [self.chatfiled resignFirstResponder];
    [self.backV removeFromSuperview];
    self.backV = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 40 * ScreenHeigth, [UIScreen mainScreen].bounds.size.width, 40 * ScreenHeigth)];
    self.backV.backgroundColor = [UIColor colorWithRed:200 / 255.0 green:200 / 255.0 blue:200 / 255.0 alpha:1.0];
    [self.view addSubview:self.backV];
    
    [self.backV addSubview:self.voiceChange];
    if (YES == self.textOrvoice) {
        [self.backV addSubview:self.chatfiled];
    } else{
        [self.backV addSubview:self.voice];
    }
    [self.backV addSubview:self.moji];
    [self.backV addSubview:self.moreFunction];
    
    
    
}
//语音切换
- (void)voiceChangeAction:(UIButton *)sender
{
    
    
    if (YES == self.clickVoice) {
        [self.backV removeFromSuperview];
        [self.chatfiled resignFirstResponder];
        [self.voiceChange setImage:[UIImage imageNamed:@"iconfont-jianpan.png"] forState:UIControlStateNormal];
        self.backV = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 40 * ScreenHeigth, [UIScreen mainScreen].bounds.size.width, 40 * ScreenHeigth)];
        self.backV.backgroundColor = [UIColor colorWithRed:200 / 255.0 green:200 / 255.0 blue:200 / 255.0 alpha:1.0];
        [self.view addSubview:self.backV];
        [self.backV addSubview:self.voice];
        [self.backV addSubview:self.voiceChange];
        [self.backV addSubview:self.moji];
        [self.backV addSubview:self.moreFunction];
        self.clickVoice = NO;
        self.textOrvoice = NO;
        
    } else {
        [self.backV removeFromSuperview];
        [self.chatfiled becomeFirstResponder];
        [self.voiceChange setImage:[UIImage imageNamed:@"iconfont-shengyin.png"] forState:UIControlStateNormal];
        self.backV = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 218 * ScreenHeigth, [UIScreen mainScreen].bounds.size.width, 40 * ScreenHeigth)];
        self.backV.backgroundColor = [UIColor colorWithRed:200 / 255.0 green:200 / 255.0 blue:200 / 255.0 alpha:1.0];
        [self.view addSubview:self.backV];
        [self.backV addSubview:self.voiceChange];
        [self.backV addSubview:self.chatfiled];
        [self.backV addSubview:self.moji];
        [self.backV addSubview:self.moreFunction];
        self.clickVoice = YES;
        self.textOrvoice = YES;
        
    }
    
    
    
}

//点击一下语音
-(void)tapAction:(UIButton *)sender{
    
    [self.view addSubview:self.backVoice];
    [self.backVoice addSubview:self.la];
    [self.la setTextColor:[UIColor blackColor]];
    
   self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerAction:) userInfo:sender repeats:YES];

    
    
}

- (void)timerAction:(NSTimer *)send
{

    [self.backVoice removeFromSuperview];
    [_myTimer invalidate];

    self.myTimer = nil;

}


//长按语音
-(void)btnLong:(UILongPressGestureRecognizer *)gestureRecognizer{
    
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        
        [self.view addSubview:self.backVoice];
        [self.backVoice addSubview:self.la];
        [self.la setTextColor:[UIColor blackColor]];
        
    }
    
    if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) {
        
        [self.backVoice removeFromSuperview];
        [self.la removeFromSuperview];
        
    }
    if ([gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        
        [self.la setText:@"松开手指取消发送"];
        [self.la setTextColor:[UIColor redColor]];
        
    }
    
}



//moji
- (void)mojiAction:(UIButton *)sender
{
    
    if (YES == self.clickMoji) {
        [self.backV removeFromSuperview];
        [self.moji setImage:[UIImage imageNamed:@"iconfont-jianpan.png"] forState:UIControlStateNormal];
        self.backV = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 218 * ScreenHeigth, [UIScreen mainScreen].bounds.size.width, 40 * ScreenHeigth)];
        self.backV.backgroundColor = [UIColor colorWithRed:200 / 255.0 green:200 / 255.0 blue:200 / 255.0 alpha:1.0];
        [self.view addSubview:self.backV];
        [self.backV addSubview:self.voiceChange];
        [self.backV addSubview:self.chatfiled];
        [self.backV addSubview:self.moji];
        [self.backV addSubview:self.moreFunction];
        self.clickMoji = NO;
        
    } else
    {
        [self.chatfiled becomeFirstResponder];
        self.clickMoji = YES;
        [self.moji setImage:[UIImage imageNamed:@"iconfont-biaoqing.png"] forState:UIControlStateNormal];
        
        
    }
    
    
    
}
//更多
- (void)moreFunctionAction:(UIButton *)sender
{
    if (YES == self.clickMoreFunction) {
        [self.backV removeFromSuperview];
        self.backV = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 218 * ScreenHeigth, [UIScreen mainScreen].bounds.size.width, 40 * ScreenHeigth)];
        self.backV.backgroundColor = [UIColor colorWithRed:200 / 255.0 green:200 / 255.0 blue:200 / 255.0 alpha:1.0];
        [self.view addSubview:self.backV];
        [self.backV addSubview:self.voiceChange];
        [self.backV addSubview:self.chatfiled];
        [self.backV addSubview:self.moji];
        [self.backV addSubview:self.moreFunction];
        self.clickMoreFunction = NO;
        
    } else
    {
        [self.chatfiled becomeFirstResponder];
        self.clickMoreFunction = YES;
        
        
    }
    
}



- (void)leftBarButtonAction:(UIBarButtonItem *)sender
{
    
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
